<?php

class Common_Chart_Strategy_Google_Bar_HorizontalGrouped extends Common_Chart_Strategy_Google_Bar {

    public function __construct() {
        parent::__construct();
    }

    public function get_chart_code() {
        return self::CHART_TYPE_BAR_HORIZONTAL_GROUPED;
    }

    public function get_chart_type() {
        return Common_Chart::TYPE_HORIZONTAL_BAR_GROUPED;
    }

    protected function apply_axis_options($arr_chart_parameters, $arr_headers, $arr_options = array()) {

        //----------------------------------------------------------------------
        // x-axis values
        //----------------------------------------------------------------------

        $int_scale_min = $this->get_data_min();
        $int_scale_max = $this->get_data_max();

        if(isset($arr_options['xaxis_min']) && is_numeric($arr_options['xaxis_min'])) {
            $int_scale_min = (int) $arr_options['xaxis_min'];
        }

        if(isset($arr_options['xaxis_max']) && is_numeric($arr_options['xaxis_max'])) {
            $int_scale_max = (int) $arr_options['xaxis_max'];
        }

        $str_chart_scale = $int_scale_min . ',' . $int_scale_max;

        $arr_chart_parameters[self::PARAMETER_CHART_DATA_SCALE] = $str_chart_scale;
        $arr_chart_parameters[self::PARAMETER_CHART_AXIS_VISIBLE] = 'x';

        $str_label_suffix = isset($arr_options['xaxis_label_suffix'])
            ? $arr_options['xaxis_label_suffix']
            : null;

        // case: y-axis values are > 1 million
        if((($int_scale_min/1000) > 1) && (($int_scale_max/1000) > 1)) {

            // shorten the length of the numbers by converting into thousands
            $int_scale_min = (int) ($int_scale_min/1000);
            $int_scale_max = (int) ($int_scale_max/1000);

            // format the axis values with a thousands separator and a 'K' suffix
            // because we have divided by 1000 in the axis scale
            //
            // 0 - first visible axis
            // N*...* - required by google
            // s - display group separators
            // K - literal suffix text

            $str_axis_style = '0N*s*K' . $str_label_suffix;

            $arr_chart_parameters[self::PARAMETER_AXIS_STYLE] = $str_axis_style;

        }

        // case: y-axis values are < 1 million
        else {

            $str_axis_style = '0N*s*' . $str_label_suffix;

            // format the axis values with a thousands separator
            $arr_chart_parameters[self::PARAMETER_AXIS_STYLE] = $str_axis_style;

        }

        // 0 (zero) denotes the first visible axis of [chxt]
        $str_chart_axis_range = '0,' . $int_scale_min . ',' . $int_scale_max;

        if(isset($arr_options['xaxis_step']) && is_numeric($arr_options['xaxis_step'])) {
            $int_step = (int) $arr_options['xaxis_step'];
            $str_chart_axis_range .= ',' . $int_step;
        }

        $arr_chart_parameters[self::PARAMETER_CHART_AXIS_RANGE] = $str_chart_axis_range;

        //----------------------------------------------------------------------
        // y-axis groups
        //----------------------------------------------------------------------

        $arr_chart_labels = array();
        foreach($arr_headers as $i => $str_label) {
            $str_label_escaped = str_replace(',', '\,', $str_label);
            $arr_chart_labels[] = 't '. $str_label_escaped . ' ,000000,0,' . $i . ',11,0';
        }
        $str_chart_labels = implode('|', $arr_chart_labels);

        $arr_chart_parameters[self::PARAMETER_CHART_MARKERS] = $str_chart_labels;

        return $arr_chart_parameters;
    }

    public function apply_grid_options($arr_chart_parameters, $arr_data, $arr_options = array()) {

        //----------------------------------------------------------------------
        // grid-lines (horizontal)
        //----------------------------------------------------------------------

        $int_chart_grid_vertical_step = 20;
        if(isset($arr_options['chart_grid_vertical_step']) && is_numeric($arr_options['chart_grid_vertical_step'])) {
            $int_chart_grid_vertical_step = (int) $arr_options['chart_grid_vertical_step'];
            $int_chart_grid_vertical_step = round((100 / $int_chart_grid_vertical_step), 5);
        }

        $int_chart_grid_horizontal_step = 0;
        if(isset($arr_options['chart_grid_horizontal_step']) && is_numeric($arr_options['chart_grid_horizontal_step'])) {
            $int_chart_grid_horizontal_step = (int) $arr_options['chart_grid_horizontal_step'];
            $int_chart_grid_horizontal_step = round((100 / $int_chart_grid_horizontal_step), 5);
        }

        $arr_chart_parameters[self::PARAMETER_CHART_GRID_LINES] = $int_chart_grid_vertical_step . ',' . $int_chart_grid_horizontal_step . ',1,5';

        return $arr_chart_parameters;
    }

}